using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;

namespace OurWedding1.Models
{ 
    public class WeddingRepository : IWeddingRepository
    {
        WeddingEntities context = new WeddingEntities();

        public IQueryable<Wedding> All
        {
            get { return context.Weddings; }
        }

        public IQueryable<Wedding> AllIncluding(params Expression<Func<Wedding, object>>[] includeProperties)
        {
            IQueryable<Wedding> query = context.Weddings;
            foreach (var includeProperty in includeProperties) {
                query = query.Include(includeProperty);
            }
            return query;
        }

        public Wedding Find(int id)
        {
            return context.Weddings.Find(id);
        }

        public void InsertOrUpdate(Wedding wedding)
        {
            if (wedding.WeddingId == default(int)) {
                // New entity
                context.Weddings.Add(wedding);
            } else {
                // Existing entity
                context.Entry(wedding).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var wedding = context.Weddings.Find(id);
            context.Weddings.Remove(wedding);
        }

        public void Save()
        {
            context.SaveChanges();
        }

        public void Dispose() 
        {
            context.Dispose();
        }
    }

    public interface IWeddingRepository : IDisposable
    {
        IQueryable<Wedding> All { get; }
        IQueryable<Wedding> AllIncluding(params Expression<Func<Wedding, object>>[] includeProperties);
        Wedding Find(int id);
        void InsertOrUpdate(Wedding wedding);
        void Delete(int id);
        void Save();
    }
}